package gui.impianto;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;

import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import core.Message;

import db.DBConnection;
import db.ImpiantoDBInteraction;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NuovoCostruttoreJPanel extends javax.swing.JPanel {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JPanel headerjPanel;
	private JTextField codicejTextField;
	private JButton chiudiFinestrajButton;
	private JButton salvajButton;
	private JTextField nomejTextField;
	private JLabel nomejLabel;
	private JLabel codicejLabel;
	private JLabel logojLabel;
	private JPanel datijPanel;
	private JPanel pulsantijPanel;
	
	private JInternalFrame containerFrame;
	private NuovoModelloJPanel modelloJPanel;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new NuovoCostruttoreJPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public NuovoCostruttoreJPanel() {
		super();
		this.modelloJPanel = null;
		initGUI();
	}
	
	public NuovoCostruttoreJPanel(JInternalFrame jInt) {
		super();
		this.containerFrame = jInt;
		this.modelloJPanel = null;
		initGUI();
	}
	
	/**
	 * Used from NuovoModelloJPanel
	 * @param jInt
	 * @param nuovoModelloJPanel
	 */
	public NuovoCostruttoreJPanel(JInternalFrame jInt, NuovoModelloJPanel nuovoModelloJPanel) {
		super();
		this.containerFrame = jInt;
		this.modelloJPanel = nuovoModelloJPanel;
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(402, 239));
			{
				headerjPanel = new JPanel();
				GridBagLayout headerjPanelLayout = new GridBagLayout();
				headerjPanelLayout.rowWeights = new double[] {0.1};
				headerjPanelLayout.rowHeights = new int[] {7};
				headerjPanelLayout.columnWeights = new double[] {0.0, 0.1};
				headerjPanelLayout.columnWidths = new int[] {243, 7};
				headerjPanel.setLayout(headerjPanelLayout);
				this.add(headerjPanel);
				headerjPanel.setPreferredSize(new java.awt.Dimension(387, 169));
				{
					datijPanel = new JPanel();
					GridBagLayout datijPanelLayout = new GridBagLayout();
					datijPanelLayout.rowWeights = new double[] {0.0, 0.1, 0.0, 0.1};
					datijPanelLayout.rowHeights = new int[] {30, 7, 22, 7};
					datijPanelLayout.columnWeights = new double[] {0.1};
					datijPanelLayout.columnWidths = new int[] {7};
					datijPanel.setLayout(datijPanelLayout);
					headerjPanel.add(datijPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					datijPanel.setPreferredSize(new java.awt.Dimension(181, 164));
					datijPanel.setBorder(BorderFactory.createTitledBorder(null, "Nuovo Costruttore", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION));
					{
						codicejLabel = new JLabel();
						datijPanel.add(codicejLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						codicejLabel.setText("Codice Costruttore (Automatico):");
						codicejLabel.setFont(new java.awt.Font("Segoe UI",3,12));
					}
					{
						nomejLabel = new JLabel();
						datijPanel.add(nomejLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						nomejLabel.setText("Nome Costruttore:");
						nomejLabel.setFont(new java.awt.Font("Segoe UI",3,12));
					}
					{
						nomejTextField = new JTextField();
						datijPanel.add(nomejTextField, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0));
					}
					{
						codicejTextField = new JTextField();
						datijPanel.add(codicejTextField, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0));
						codicejTextField.setEditable(false);
						codicejTextField.setEnabled(false);
					}
				}
				{
					logojLabel = new JLabel();
					headerjPanel.add(logojLabel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					logojLabel.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/icons/costruttore128.png")));
				}
			}
			{
				pulsantijPanel = new JPanel();
				this.add(pulsantijPanel);
				pulsantijPanel.setPreferredSize(new java.awt.Dimension(386, 60));
				{
					salvajButton = new JButton();
					pulsantijPanel.add(salvajButton);
					salvajButton.setText("Salva Nuovo Costruttore");
					salvajButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/icons/save-icon32.png")));
					salvajButton.addActionListener(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent e) {
							try {
								DBConnection dbCon = new DBConnection();
								// TODO Controllare prima che il nome del costruttore non sia gia stato inserito nel db
								if(ImpiantoDBInteraction.ControlIfAlreadyExistsASimilarCostruttore(nomejTextField.getText(), dbCon))
								{
									ImpiantoDBInteraction.SaveNewCostruttore(nomejTextField.getText(), dbCon);
									nomejTextField.setText("");
								}
							} catch (SQLException e1) {
								e1.printStackTrace();
							}
						}
					});
				}
				{
					chiudiFinestrajButton = new JButton();
					pulsantijPanel.add(chiudiFinestrajButton);
					chiudiFinestrajButton.setText("Chiudi Finestra");
					chiudiFinestrajButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/icons/close32.png")));
					chiudiFinestrajButton.addActionListener(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent arg0) {
							closeInternalFrame();
						}
					});
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * This function allows to close the current container JInternalFrame.
	 * If nomejTextField is Empty it closes directly the frame;
	 * if nomejTextField is not Empty, it asks if the user is sure to close the window or not.
	 */
	private void closeInternalFrame()
	{
		if(!nomejTextField.getText().isEmpty())
		{
			if(Message.yesNoMessage("Attenzione, i dati non salvati andranno persi, chiudere la finestra?", "Chiusura Finestra Corrente"))
			{
				this.containerFrame.dispose();
				if(modelloJPanel != null)
					modelloJPanel.reloadCostruttoreJComboBox();
			}
		}
		else
		{
			this.containerFrame.dispose();
			if(modelloJPanel != null)
				modelloJPanel.reloadCostruttoreJComboBox();
		}
	}
}
